package com.lizicloud.infrastructure.common.utils;

import java.util.UUID;
import java.security.SecureRandom;

/**
 * ID生成工具类
 */
public class IdUtils {
    
    private static final SecureRandom RANDOM = new SecureRandom();
    private static final char[] CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
    
    /**
     * 生成UUID
     */
    public static String generateUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }
    
    /**
     * 生成简单ID（纯数字）
     */
    public static long generateSimpleId() {
        return System.currentTimeMillis() * 10000 + RANDOM.nextInt(10000);
    }
    
    /**
     * 生成随机字符串ID
     */
    public static String generateRandomId(int length) {
        if (length <= 0) {
            throw new IllegalArgumentException("长度必须大于0");
        }
        
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            sb.append(CHARS[RANDOM.nextInt(CHARS.length)]);
        }
        return sb.toString();
    }
    
    /**
     * 生成订单号
     */
    public static String generateOrderNo() {
        return "ORD" + System.currentTimeMillis() + String.format("%04d", RANDOM.nextInt(10000));
    }
    
    /**
     * 生成学生学号
     */
    public static String generateStudentNo() {
        return "STU" + System.currentTimeMillis() + String.format("%04d", RANDOM.nextInt(10000));
    }
    
    /**
     * 生成随机验证码
     */
    public static String generateVerificationCode(int length) {
        if (length <= 0) {
            throw new IllegalArgumentException("长度必须大于0");
        }
        
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            sb.append(RANDOM.nextInt(10));
        }
        return sb.toString();
    }
    
    /**
     * 生成随机密码
     */
    public static String generateRandomPassword(int length) {
        if (length < 6) {
            throw new IllegalArgumentException("密码长度至少为6位");
        }
        
        StringBuilder sb = new StringBuilder(length);
        
        // 至少包含一个大写字母
        sb.append(CHARS[RANDOM.nextInt(26)]);
        
        // 至少包含一个小写字母
        sb.append(CHARS[RANDOM.nextInt(26) + 26]);
        
        // 至少包含一个数字
        sb.append(CHARS[RANDOM.nextInt(10) + 52]);
        
        // 剩余位置随机填充
        for (int i = 3; i < length; i++) {
            sb.append(CHARS[RANDOM.nextInt(CHARS.length)]);
        }
        
        // 打乱顺序
        char[] chars = sb.toString().toCharArray();
        for (int i = 0; i < chars.length; i++) {
            int j = RANDOM.nextInt(chars.length);
            char temp = chars[i];
            chars[i] = chars[j];
            chars[j] = temp;
        }
        
        return new String(chars);
    }
    
    /**
     * 检查UUID格式
     */
    public static boolean isValidUUID(String uuid) {
        if (uuid == null || uuid.length() != 32) {
            return false;
        }
        
        try {
            UUID.fromString(uuid.substring(0, 8) + "-" + 
                           uuid.substring(8, 12) + "-" + 
                           uuid.substring(12, 16) + "-" + 
                           uuid.substring(16, 20) + "-" + 
                           uuid.substring(20, 32));
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    
    private IdUtils() {
        // 私有构造函数，防止实例化
    }
}